package com.gaga.console.base.config;

import com.gaga.console.base.utils.DatabasePath;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.sqlite.SQLiteDataSource;

import javax.sql.DataSource;

/**
 * @DESCRIPTION:
 * @USER: ningxiang
 * @DATE: 4/26/2025 11:24 AM
 */
@Configuration
@Slf4j
public class DatabaseConfig {

    @Value("${database.path:}")
    private String customPath;

    @Bean
    public String databasePath() {
        if (!customPath.isEmpty()) {
            return customPath;  // 优先使用外部指定的路径
        }
        return DatabasePath.getPath();  // 动态生成路径
    }

    @Bean
    public DataSource dataSource() {
        SQLiteDataSource dataSource = new SQLiteDataSource();
        dataSource.setUrl("jdbc:sqlite:file:" + databasePath() + "?cache=shared");
        log.info("=====dbPath=: " + databasePath());
        return dataSource;
    }
}